package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallCategory;
import com.ctsi.ssdc.domain.LitemallCategoryExample;
import com.ctsi.ssdc.service.LitemallCategoryService;
import com.ctsi.ssdc.repository.LitemallCategoryRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallCategory.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCategoryServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallCategoryRepository, LitemallCategory, Integer, LitemallCategoryExample> 
	implements LitemallCategoryService {

    @Autowired
    LitemallCategoryRepository litemallCategoryRepository;
    private LitemallCategory.Column[] CHANNEL = {LitemallCategory.Column.id, LitemallCategory.Column.name, LitemallCategory.Column.iconUrl};


    /**
    * GET  /litemallCategorys : get the litemallCategorys firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallCategory> findFirstStringColumn(String name,Pageable pageable){
        String str = name;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallCategoryExample litemallCategoryExample = new LitemallCategoryExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallCategoryExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallCategoryExample.createCriteria();
        } else{
            litemallCategoryExample.createCriteria().andNameLike("%" + str +"%");
        }
        List<LitemallCategory>  data = litemallCategoryRepository.selectByExample(litemallCategoryExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallCategoryRepository.countByExample(litemallCategoryExample);
        }
        return new PageResult<LitemallCategory>(data,count,count);
    }

    @Override
    public List<LitemallCategory> queryL1() {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return litemallCategoryRepository.selectByExample(example);
    }

    @Override
    public List<LitemallCategory> queryByPid(Integer id) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andPidEqualTo(id).andDeletedEqualTo(false);
        return litemallCategoryRepository.selectByExample(example);
    }

    @Override
    public LitemallCategory findById(Integer id) {
        return litemallCategoryRepository.selectByPrimaryKey(id);
    }

    @Override
    public List<LitemallCategory> queryL1WithoutRecommend(int offset, int limit) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andNameNotEqualTo("推荐").andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        return litemallCategoryRepository.selectByExample(example);
    }

    @Override
    public List<LitemallCategory> queryChannel() {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return litemallCategoryRepository.selectByExampleSelective(example, CHANNEL);
    }

    @Override
    public List<LitemallCategory> queryL2ByIds(List<Integer> goodsCatIds) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andIdIn(goodsCatIds).andLevelEqualTo("L2").andDeletedEqualTo(false);
        return litemallCategoryRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
